package org.yonggan.shop.sql.report

import org.apache.spark.sql.{DataFrame, Row}
import org.yonggan.shop.constant.ConfigurationManager
import org.yonggan.shop.utils.SparkUtils

/**
  * 支付漏斗转化率
  *
      step1 查看商品详细信息
      step2 加入购物车
      step3 开始支付
      step4 支付成功

  */
object RptPayFunnelConverRate {

  def main(args: Array[String]): Unit = {

    val spark = SparkUtils.getSparkSession("支付漏斗转化率")

    // 文件输出
    val jsonDF: DataFrame = spark.read.json(ConfigurationManager.TASK_INJSON)
    jsonDF.createTempView("v_all_data")

    val baseDF = spark.sql(
      """
        |SELECT event_type, openid, pay_status  FROM v_all_data
        |WHERE  event_type IS NOT NULL AND openid IS NOT NULL
      """.stripMargin).cache()

    // kpi指标
    baseDF.createTempView("v_pay_funnel")

    val brCartRate = spark.sql(
      """
        |SELECT event_type as step , count(1) as ctn FROM v_pay_funnel
        |WHERE event_type = 1 OR event_type = 3 OR event_type = 4
        |GROUP BY event_type ORDER BY event_type
      """.stripMargin)
    // 浏览和添加购物车
    var resultArr: Array[Row] = brCartRate.collect()

    //支付成功
    val succPay = spark.sql(
      """
       |SELECT  pay_status , count(1) as ctn
       |FROM v_pay_funnel WHERE pay_status = '1'
       |GROUP BY pay_status
      """.stripMargin).collect()

     resultArr ++= succPay

    resultArr.foreach(println)

//    [1,357]    step1  查看商品详细信息
//    [3,229]    step2  加入购物车
//    [4,49]     step3  开始支付
//    [1,39]     step4  支付成功
    spark.stop()
  }

}
